﻿#region Legal

// Jimmy Zimmerman
// Team Mongoose
//
// END USER LICENSE AGREEMENT
// IMPORTANT - READ THIS CAREFULLY:  This End User License Agreement is a legal agreement
// between you (either an individual, business entity, currently active identity of an
// individual with Multiple Personality Disorder, octopus overlord, or otherworldly entity),
// and Team Mongoose, for the enclosed, components.
//
// By reading this document and/or installing this product, you agree to be bound by the
// terms of this EULA.
//
// Team Mongoose owns all copyright, trade secret, trademark, trade wars,
// patent, portent, and potential rights to these components.  Team Mongoose
// grants you the right to deploy the enclosed components.
//
// If you agree to the terms of this EULA, a license to use these components is granted to you.
//
// If you should happen to benefit from the enclosed components, then you are legally and
// contractually bound to thank us for it. Send your regards to jimmyzimms@hotmail.com.
//
// OTHER RIGHTS AND LIMITATONS
// You may not reverse-engineer, decompile, decode, disassemble, psycho-analyze, or otherwise
// attempt to find hidden meanings between the lines of this EULA; unless, of course, you should
// happen to find some, and they are funny.
//
// You may not translate this EULA or any part of the components into Ancient Sumerian.
//
// THESE COMPONENTS ARE PROVIDED “AS-IS” WITHOUT WARRANTY OF ANY KIND. ANY USE OF THE COMPONENTS
// CONTAINED IS AT YOUR OWN RISK. TEAM MONGOOSE DISCLAIM ALL WARRANTIES, EITHER
// EXPRESS OR IMPLIED, WITH RESPECT TO THE ACCURRACY AND CORRECTNESS OF THE COMPONENTS CONTAINED
// HEREIN. TEAM MONGOOSE DOES NOT WARRANT THAT THE COMPONENTS ARE FLAWLESS.
//
// REDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS, WITH OR WITHOUT MODIFICATION, ARE PERMITTED
// PROVIDED THAT THE FOLLOWING CONDITIONS ARE MET:
// * REDISTRIBUTIONS OF SOURCE CODE MUST RETAIN THE ABOVE COPYRIGHT NOTICE
// * REDISTRIBUTIONS IN BINARY FORM MUST NOTE THE USE OF THE COMPONENT IN DOCUMENTATION AND/OR
//   OTHER MATERIALS PROVIDED WITH THE DISTRIBUTION.
// * NEITHER THE NAME OF TEAM MONGOOSE MAY BE USED TO ENDORES OR PROMOTE PRODUCTS
//   DERIVED FROM THIS SOFTWARE WITHOUT SPECIFIC PRIOR WRITTEN PERMISSION.
//
// IN NO EVENT SHALL TEAM MONGOOSE BE HELD LIABLE FOR INCIDENTAL, SPECIAL, INDIRECT,
// INCONSEQUENTIAL, UNBELIEVABLE, EXAGGERATED, VERBOSE, OR TYPICAL DAMAGES INCURRED WHILE USING
// THE ENCLOSED COMPONENTS.
//
// OUR STUFF ALWAYS WORKS - SOMETIMES.

#endregion

using Microsoft.AnalysisServices.AdomdClient;
using System.ComponentModel;
using System.Data.Common;

namespace System.Data.MdxClient
{
    /// <summary>
    /// Represents a MDX data adapter.
    /// </summary>
    public class MdxDataAdapter : DbDataAdapter
    {
        #region Fields

        private readonly AdomdDataAdapter adapter;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the internal, native ADOMD.NET data adapter.
        /// </summary>
        /// <value>An <see cref="AdomdDataAdapter"/> object.</value>
        protected internal AdomdDataAdapter AdomdDataAdapter
        {
            get
            {
                return this.adapter;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the object has been disposed.
        /// </summary>
        /// <value>True if the object has been disposed; otherwise, false.s</value>
        protected bool IsDisposed
        {
            get;
            private set;
        }

        #endregion

        #region Overrides

        /// <summary>
        /// Creates a new <see cref="DataTableMappingCollection"/>.
        /// </summary>
        /// <returns>A <see cref="DataTableMappingCollection"/> object.</returns>
        protected override DataTableMappingCollection CreateTableMappings()
        {
            return this.adapter.TableMappings;
        }

        /// <summary>
        /// Releases the managed and, optionally, the unmanaged resources used by the <see cref="MdxDataAdapter"/> class.
        /// </summary>
        /// <param name="disposing">Indicates whether the object is being disposed.</param>
        protected override void Dispose( bool disposing )
        {
            if ( this.IsDisposed )
                return;

            this.IsDisposed = true;
            this.adapter.Dispose();
            base.Dispose( disposing );
        }

        /// <summary>
        /// Adds or refreshes rows in the DataSet to match those in the data source using the DataSet name, and creates a DataTable.
        /// </summary>
        /// <param name="dataSet">A DataSet to fill with records and, if necessary, schema.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected
        /// by statements that do not return rows.</returns>
        public override int Fill( DataSet dataSet )
        {
            return this.adapter.Fill( dataSet );
        }

        /// <summary>
        /// Adds a DataTable to the specified DataSet and configures the schema to match that in the data
        /// source based on the specified SchemaType.
        /// </summary>
        /// <param name="dataSet">The DataSet to be filled with the schema from the data source.</param>
        /// <param name="schemaType">One of the SchemaType values.</param>
        /// <returns>A DataTable object that contains schema information returned from the data source.</returns>
        public override DataTable[] FillSchema( DataSet dataSet, SchemaType schemaType )
        {
            return this.adapter.FillSchema( dataSet, schemaType );
        }

        /// <summary>
        /// Gets the parameters set by the user when executing an SQL SELECT statement.
        /// </summary>
        /// <returns>An array of <see cref="IDataParameter"/> objects that contains the parameters set by the user.</returns>
        public override IDataParameter[] GetFillParameters()
        {
            return this.adapter.GetFillParameters();
        }

        /// <summary>
        /// Gets or sets whether the Fill method should return provider-specific values or common CLS-compliant values.
        /// </summary>
        /// <value>True if the Fill method should return provider-specific values; otherwise false to return common CLS-compliant values.</value>
        public override bool ReturnProviderSpecificTypes
        {
            get
            {
                return this.adapter.ReturnProviderSpecificTypes;
            }
            set
            {
                this.adapter.ReturnProviderSpecificTypes = value;
            }
        }

        /// <summary>
        /// Returns a lifetime service object to maintain the lifetime of the component.
        /// </summary>
        /// <returns>A lifetime service object.</returns>
        public override object InitializeLifetimeService()
        {
            return this.adapter.InitializeLifetimeService();
        }

        /// <summary>
        /// Gets or sets the site of the component.
        /// </summary>
        /// <value>An <see cref="ISite"/> object.</value>
        public override ISite Site
        {
            get
            {
                return this.adapter.Site;
            }
            set
            {
                this.adapter.Site = value;
            }
        }

        /// <summary>
        /// Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted
        /// row in the specified <see cref="DataSet"/> from a <see cref="DataTable"/> named "Table".
        /// </summary>
        /// <param name="dataSet">The <see cref="DataSet"/> used to update the data source.</param>
        /// <returns>The number of rows successfully updated from the <see cref="DataSet"/>.</returns>
        public override int Update( DataSet dataSet )
        {
            return this.adapter.Update( dataSet );
        }

        /// <summary>
        /// Gets or sets a value that enables or disables batch processing support, and specifies the number of
        /// commands that can be executed in a batch.
        /// </summary>
        /// <value>The number of rows to process per batch. The <see cref="DbDataAdapter"/> will use the largest
        /// batch size that the server can handle.</value>
        public override int UpdateBatchSize
        {
            get
            {
                return this.adapter.UpdateBatchSize;
            }
            set
            {
                this.adapter.UpdateBatchSize = value;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Instantiates a new instance of the <see cref="MdxDataAdapter"/> class.
        /// </summary>
        /// <param name="adapter">The adapted <see cref="AdomdDataAdapter"/> object.</param>
        protected internal MdxDataAdapter( AdomdDataAdapter adapter )
        {
            if ( ( this.adapter = adapter ) == null )
                throw new ArgumentNullException( "adapter" );
        }

        /// <summary>
        /// Instantiates a new instance of the <see cref="MdxDataAdapter"/> class.
        /// </summary>
        public MdxDataAdapter()
            : this( new AdomdDataAdapter() )
        {
        }

        #endregion
    }
}
